﻿using Leeder.Business;
using Leeder.Model;
using LogisticsAndongApi.Business;
using LogisticsAndongApi.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Results;
using System.Web.Mvc;

namespace LogisticsAndongApi.Controllers
{
    public class MaterialDirectionController : ApiControllerBase
    {
        // GET api/values
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        /// <summary>
        /// 获取工程数量
        /// </summary>
        /// <param name="project_code">工程编码</param>
        /// <returns></returns>
        [System.Web.Http.HttpGet]
        [System.Web.Http.Route("get_project")]
        public ActionResult GetProject(string project_code)
        {
            var ret= BF<MaterialDirectionBLL, MaterialDirectionModel>.Query(p => p.MaterialCode==project_code);
            if (!ret.IsSuccess)
            {
                return Json(ErrorResult<ProjectDto>("100", "查询报错了"), JsonRequestBehavior.AllowGet);
                //return ErrorResult<ProjectDto>("100","查询报错了");
            }
            var materialDirectionModel = ret.ActivedObj.FirstOrDefault();
            if (materialDirectionModel == null)
            {
                return Json(ErrorResult<ProjectDto>("101", "工程不存在"), JsonRequestBehavior.AllowGet);
                //return ErrorResult<ProjectDto>("101", "工程不存在");
            }

            var project = new ProjectDto(){ ProjectCode= materialDirectionModel.MaterialCode,ProjectName= materialDirectionModel.MaterialName,LeftCount= materialDirectionModel.LeftCount };

            //return SuccessResult(project);
            return Json(SuccessResult(project), JsonRequestBehavior.AllowGet);
            //return JsonConvert.SerializeObject(project) ;
        }

        // POST api/values
        public void Post([FromBody]string value)
        {
        }

        // PUT api/values/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/values/5
        public void Delete(int id)
        {
        }
    }
}
